package com.centling.smartSealForPhone.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.application.SmartSeal
import com.centling.smartSealForPhone.utils.ShowToast
import kotlinx.android.synthetic.privateCloud.activity_set_server_ip.*

/**
 * 设置Ip
 */
class SetServerIpActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_set_server_ip)

        initView()
    }

    fun initView() {
        et_set_server_ip.setText(SmartSeal.getInstance().ip)
        tv_set_server_sure.setOnClickListener {
            var newIp = et_set_server_ip.text.toString()
            if (!TextUtils.isEmpty(newIp)) {
                if(!newIp.startsWith(PREFIX_HTTPS) && !newIp.startsWith(PREFIX_HTTP)) {
                    newIp = "$PREFIX_HTTP$newIp"
                }
                SmartSeal.getInstance().changeIp(newIp)
                setResult(Activity.RESULT_OK,  Intent().putExtra("ip", newIp))
                finish()
            } else {
                ShowToast.shortTime("服务器地址不能为空")
            }
        }
        tv_set_server_cancel.setOnClickListener { finish() }
    }

    override fun onBackPressed() {
        setResult(Activity.RESULT_CANCELED)
        finish()
    }

    companion object {
        val PREFIX_HTTP = "http://"
        val PREFIX_HTTPS = "https://"
    }
}
